using System;
using System.Collections;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	public class ValidationSummary : System.Web.UI.WebControls.ValidationSummary, IContent
	{
		private IValidationSummaryConfiguration validationSummaryConfiguration;
		private string configurationId;
		private IConfiguration configuration;

		private String contentPlaceHolderId;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public IValidationSummaryConfiguration ValidationSummaryConfiguration
		{
			get { return validationSummaryConfiguration; }
			set { validationSummaryConfiguration = value; }
		}

		public string ConfigurationId
		{
			get { return configurationId; }
			set { configurationId = value; }
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { configuration = value; }
		}

		public ValidationSummary()
		{
		}

		public ValidationSummary(IConfiguration configuaration)  : this()
		{
			this.configuration = configuaration;
			this.configurationId = configuration.ObjectName;
			BuildControlFromConfiguration();
		}

		protected virtual void BuildControlFromConfiguration()
		{
			this.ID = configuration.Id;

			IValidationSummaryConfiguration validationSummaryConfiguration = configuration as IValidationSummaryConfiguration;
		    
			if (validationSummaryConfiguration != null)
			{
				if (validationSummaryConfiguration.DisplayMode != null && !string.Empty.Equals(validationSummaryConfiguration.DisplayMode))
				{
                    DisplayMode = (ValidationSummaryDisplayMode)System.Enum.Parse(typeof(ValidationSummaryDisplayMode), validationSummaryConfiguration.DisplayMode);
				}
			}

            ValidationGroup = configuration.ValidationGroup;

            if (!string.IsNullOrEmpty(validationSummaryConfiguration.CssDisplay))
                this.Style.Add("display", validationSummaryConfiguration.CssDisplay);
		}

		#region Localization Logic

		private string headerTextKey;


		protected override void OnInit(EventArgs e)
		{
			Initialize();
			base.OnInit (e);
		}

		public void Initialize()
		{
			base.HeaderText = ControlUtil.ExtractMessageManager(this).GetMessage(headerTextKey!=null&&headerTextKey!=string.Empty?headerTextKey:(this.ID+".headerTextKey"), base.HeaderText);
		}

		public string HeaderTextKey
		{
			get { return headerTextKey; }
			set { headerTextKey = value; }
		}

		#endregion Localization Logic
	}
}
